/*
 * @Author: liutongchao 1923236391@qq.com
 * @Date: 2023-05-31 13:44:24
 * @LastEditors: liutongchao 1923236391@qq.com
 * @LastEditTime: 2025-03-24 14:30:41
 * @FilePath: \greenbeeadmin\controllers\admin\service\userService.go
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package service

import (
	"greenbeeadmin/models"

	greebeeUtils "greenbeeadmin/controllers/greenbee/utils"

	"github.com/beego/beego/v2/client/orm"
)

/**
 * @description: 系统菜单 service
 * @return {*}
 */
type SystemMenuService struct {
	AdminBaseService
}

// 初始化 系统菜单结构体
type SystemMenuInit struct {
	HomeInfo struct {
		Title string `json:"title"`
		Href  string `json:"href"`
	} `json:"homeInfo"`
	LogoInfo struct {
		Title string `json:"title"`
		Image string `json:"image"`
	} `json:"logoInfo"`
	MenuInfo []*MenuTreeList `json:"menuInfo"`
}

// 系统菜单结构体
type MenuTreeList struct {
	Id     int             `json:"id"`
	Pid    int             `json:"pid"`
	Title  string          `json:"title"`
	Icon   string          `json:"icon"`
	Href   string          `json:"href"`
	Target string          `json:"target"`
	Remark string          `json:"remark"`
	Child  []*MenuTreeList `json:"child"`
}

// 获取初始化数据
func (m *SystemMenuService) GetSystemInit(adminUserId int) SystemMenuInit {
	var systemInit SystemMenuInit

	// 首页
	systemInit.HomeInfo.Title = "首页"
	systemInit.HomeInfo.Href = "admin/main"

	// logo
	systemInit.LogoInfo.Title = "GreenBeeAdmin"
	systemInit.LogoInfo.Image = "/static/images/logo.jpg"

	// 菜单
	systemInit.MenuInfo = m.GetMenuList(adminUserId)

	return systemInit
}

// 获取菜单列表
func (m *SystemMenuService) GetMenuList(adminUserId int) []*MenuTreeList {

	tableName := new(models.AdminMenu).GetTableName()
	var menuList []models.AdminMenu

	//_, _ = o.QueryTable(adminMenu.TableName()).Filter("is_menu", 0).Filter("status", 0).OrderBy("-order_number").All(&menuList)
	where := "status = 0 and is_menu = 0"
	//查询数据
	o := orm.NewOrm()
	qb, _ := orm.NewQueryBuilder("mysql")
	// 构建查询对象
	qb.Select("id,created_at,updated_at,authority_name,menu_icon,parent_id,order_number,menu_url,authority,is_menu,status").
		From(tableName).
		Where(where).OrderBy("order_number").Desc()

	// 导出 SQL 语句
	sql := qb.String()

	// 执行 SQL 语句
	o.Raw(sql).QueryRows(&menuList)
	// 获取用户的权限菜单id
	var adminUserService AdminUserService
	authorizationIds := adminUserService.GetUserAuthorizationIds(adminUserId)
	return m.buildMenuChild(-1, menuList, authorizationIds)
}

// 递归获取子菜单
func (m *SystemMenuService) buildMenuChild(pid int, menuList []models.AdminMenu, authorizationIds []int) []*MenuTreeList {
	var treeList []*MenuTreeList
	for _, v := range menuList {
		if pid == v.ParentId {
			node := &MenuTreeList{
				Id:     v.Id,
				Title:  v.AuthorityName,
				Icon:   v.MenuIcon,
				Href:   v.MenuUrl,
				Target: "_self",
				Pid:    v.ParentId,
			}
			child := m.buildMenuChild(v.Id, menuList, authorizationIds)
			if len(child) != 0 {
				node.Child = child
			}
			// 此处加上用户的权限判断
			if greebeeUtils.InArrayInt(v.Id, authorizationIds) {
				treeList = append(treeList, node)
			}

		}
	}
	return treeList
}
